use axum::Router;
use dotenv::dotenv;
use std::{env, net::{Ipv4Addr, SocketAddrV4}};

pub async fn run(router: Router) {
    dotenv().ok();
    let env_host = env::var("HOST").unwrap_or_default( );
    let env_port = env::var("PORT").unwrap_or_default( );
    let host = if env_host.is_empty() { config::HOST } else { &env_host };
    let port = if env_port.is_empty() { config::PORT } else { &env_port };

    let host = host.split(".").collect::<Vec<&str>>();
    let mut ip = vec![];
    for v in host{
        let x = v.parse::<u8>().unwrap_or_default();
        ip.push(x);
    }

    let addr = SocketAddrV4::new(Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3] ), port);

    let listener = tokio::net::TcpListener::bind( addr ).await.unwrap();
    axum::serve(listener, router.into_make_service()).await.unwrap_or_default()
}